<template>
    <div class="admin-profile">
        <el-card shadow="never" class="profile-card">
            <template #header>
                <div class="card-header">
                    <span class="header-title">管理员资料</span>
                    <el-button type="primary" @click="editDialogVisible = true">修改信息</el-button>
                </div>
            </template>

            <div class="profile-content">
                <!-- 基本信息 -->
                <div class="info-section">
                    <h3 class="section-title">基本信息</h3>
                    <div class="info-list">
                        <div class="info-item">
                            <span class="info-label">管理员ID：</span>
                            <span class="info-value">{{ adminStore.adminInfo.admin_id || '-' }}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">管理员名称：</span>
                            <span class="info-value">{{ adminStore.adminInfo.admin_name || '-' }}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">邮箱：</span>
                            <span class="info-value">{{ adminStore.adminInfo.email || '-' }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </el-card>

        <!-- 修改信息对话框 -->
        <el-dialog v-model="editDialogVisible" title="修改管理员信息" width="500px" destroy-on-close>
            <el-form ref="profileFormRef" :model="profileForm" :rules="profileRules" label-width="100px">
                <el-form-item label="管理员ID">
                    <el-input v-model="adminStore.adminInfo.admin_id" disabled />
                </el-form-item>
                <el-form-item label="管理员名称" prop="admin_name">
                    <el-input v-model="profileForm.admin_name" placeholder="请输入管理员名称" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="profileForm.email" placeholder="请输入邮箱" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="editDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleSaveProfile" :loading="saveLoading">
                    保存
                </el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useAdminStore } from '@/stores/admin'
import { validateEmail } from '@/utils/validate'

const adminStore = useAdminStore()
const profileFormRef = ref()
const saveLoading = ref(false)
const editDialogVisible = ref(false)

// 表单数据
const profileForm = reactive({
    admin_name: '',
    email: ''
})

// 表单验证规则
const profileRules = {
    admin_name: [
        { required: true, message: '请输入管理员名称', trigger: 'blur' },
        { min: 2, max: 20, message: '管理员名称长度在 2 到 20 个字符', trigger: 'blur' }
    ],
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (!value) {
                    callback(new Error('请输入邮箱'))
                } else if (!validateEmail(value)) {
                    callback(new Error('请输入正确的邮箱格式'))
                } else {
                    callback()
                }
            }, trigger: 'blur'
        }
    ]
}

// 初始化表单数据
const initFormData = () => {
    profileForm.admin_name = adminStore.adminInfo.admin_name || ''
    profileForm.email = adminStore.adminInfo.email || ''
}

// 重置表单
const resetForm = () => {
    initFormData()
    if (profileFormRef.value) {
        profileFormRef.value.clearValidate()
    }
}

// 保存资料
const handleSaveProfile = async () => {
    if (!profileFormRef.value) return

    try {
        await profileFormRef.value.validate()
        saveLoading.value = true

        await adminStore.updateAdminAction(adminStore.getAdminId(), profileForm)

        ElMessage.success('资料更新成功')

        // 刷新管理员信息
        await adminStore.refreshAdminInfo()

        // 关闭对话框
        editDialogVisible.value = false

    } catch (error) {
        if (error.errors) {
            ElMessage.error('请完善表单信息')
        } else {
            ElMessage.error(error.message || '资料更新失败')
            console.error('资料更新失败:', error)
        }
    } finally {
        saveLoading.value = false
    }
}

// 打开对话框时初始化表单
const handleOpenDialog = () => {
    initFormData()
    editDialogVisible.value = true
}

onMounted(() => {
    initFormData()
})
</script>

<style scoped>
.admin-profile {
    padding: 20px;
}

.profile-card {
    max-width: 800px;
    margin: 0 auto;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
}

.profile-content {
    padding: 10px 0;
}

.section-title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e4e7ed;
}

.info-section {
    margin-bottom: 40px;
}

.info-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.info-item {
    display: flex;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
    border-bottom: none;
}

.info-label {
    width: 120px;
    font-weight: 500;
    color: #606266;
}

.info-value {
    flex: 1;
    color: #303133;
}

:deep(.el-form-item) {
    margin-bottom: 22px;
}

:deep(.el-input) {
    max-width: 300px;
}

:deep(.el-input.is-disabled .el-input__inner) {
    background-color: #f5f7fa;
    color: #909399;
}
</style>